package com.qf.blog.controller;

import com.qf.blog.constatns.LoginConstatns;
import com.qf.blog.entity.Email;
import com.qf.blog.entity.User;
import com.qf.blog.service.IUserService;
import com.qf.blog.utils.BlogUtils;
import com.qf.blog.utils.EmailUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@Controller
public class CommonController {

    @Autowired
    private IUserService userService;

    @Autowired
    private EmailUtils emailUtils;

    @RequestMapping("/register")
    public String register(User user , Model model){
        String email = user.getEmail();
        User user1 = userService.getUserByEmail(email);
        if(user1 != null){
            throw new RuntimeException("邮箱号已注册");
        }

        user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));

        String code = BlogUtils.getUUID();
        user.setStatus(0);
        user.setActivationCode(code);
        user.setCreateTime(new Date());
        userService.save(user);

        Email email1 = new Email();
        email1.setTo("3301984645@qq.com");
        email1.setTitle("Blog新用户注册激活邮件");
        email1.setContent("这里有优邮件的内容<a href = 'http://127.0.0.1:8001/blog-web/activateAccount?code="+code+"'>点击这里</a>激活");
        emailUtils.send(email1);
        model.addAttribute("msg","账户注册成功，请进入您的邮箱进行激活，激活的连接已发入您的邮箱！");
        return "site/success";
    }

    @RequestMapping("/activateAccount")
    public String activateAccount(String code ,Model model){
        Integer status = userService.updateUserStatus(code,1);
        if(status > 0){
            return "site/login.html";
        }
        model.addAttribute("msg","激活码有误，激活失败");

        return "site/success";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request){
        if(StringUtils.isEmpty(username)){
            throw new RuntimeException("用户名不能为空");
        }
        if(StringUtils.isEmpty(password)){
            throw new RuntimeException("密码不能为空");
        }
        User user = userService.getUserByUserName(username);

        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }

        if (!BCrypt.checkpw(password, user.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }
        if(user.getStatus() != 1){
            throw new RuntimeException("账号未激活");
        }
        user.setPassword(null);
        request.getSession().setAttribute("loginUser", user);
        return "redirect:/";
    }

    @RequestMapping("/logOut")
    public String logOut(HttpSession session){
        Object loginUser = session.getAttribute(LoginConstatns.LOGIN_USER_KEY);
        if(loginUser == null){
            throw new RuntimeException("请先登录");
        }
        session.removeAttribute(LoginConstatns.LOGIN_USER_KEY);
        session.invalidate();
        return "site/login";

    }
}
